using UnityEngine;
using QFramework;
using UnityEngine.Video;
using System;
using System.Collections.Generic;

namespace ProjectSurvivor
{
  
	public partial class EnemyGenerator : ViewController
	{
        [SerializeField]
        public LevelConfig Config;
        
		private float mCurrentSecond = 0;
        private float mCurrentWaveSecond = 0;
        private Queue<EnemyWave> enemyWaves = new Queue<EnemyWave>();
        private int WaveCount = 0;

        private int mTotalCount = 0;
        public bool LastWave => WaveCount == mTotalCount;
        public EnemyWave CurrentWave => mCurrentWave;

        public static BindableProperty<int> enemyCount = new BindableProperty<int>(0);
        private void Start()
        {
            foreach (var group in Config.EnemyWaveGroups)
            {
                foreach (var item in group.Waves)
                {
                    enemyWaves.Enqueue(item);
                    mTotalCount++;
                }
            }
        }
        private EnemyWave mCurrentWave = null;
        private void Update()
        {
            if(mCurrentWave == null)
            {
                if(enemyWaves.Count>0)
                {
                    mCurrentWave = enemyWaves.Dequeue();
                    mCurrentSecond = 0;
                    mCurrentWaveSecond = 0;
                }
            }

            if(mCurrentWave!=null)
            {
                mCurrentSecond += Time.deltaTime;
                mCurrentWaveSecond += Time.deltaTime;
                if (mCurrentSecond>= mCurrentWave.GenerateDuration)
                {
                    mCurrentSecond = 0;
                    var player = Player.Default;
                    if (player)
                    {
                        var xOry = RandomUtility.Choose(-1,1);
                        var pos = Vector2.zero;
                        if(xOry == -1)
                        {
                            pos.x = RandomUtility.Choose(CameraController.LBTrans.position.x, CameraController.RTTrans.position.x);
                            pos.y = UnityEngine.Random.Range(CameraController.LBTrans.position.y, CameraController.RTTrans.position.y);
                        }
                        else
                        {
                            pos.x = UnityEngine.Random.Range(CameraController.LBTrans.position.x, CameraController.RTTrans.position.x);
                            pos.y = RandomUtility.Choose(CameraController.LBTrans.position.y, CameraController.RTTrans.position.y);
                        }
                        //var randomAngle = UnityEngine.Random.Range(0, 360f);
                        //var randomRadius = randomAngle * Mathf.Deg2Rad;
                        //var direction = new Vector3(Mathf.Cos(randomRadius), Mathf.Sin(randomRadius));
                        //var generatePos = player.transform.position + direction * 10;
                        mCurrentWave.enmeyprefab.Instantiate().Position(pos)
                            .Self((self)=>
                            {
                                var enemy = self.GetComponent<IEnemy>();
                                enemy.SetSpeedScale(mCurrentWave.SpeedScale);
                                enemy.SetHpScale(mCurrentWave.HpScale);
                            })
                            .Show();
                    }
                }
                if (mCurrentWaveSecond >= mCurrentWave.Seconds)
                {
                    mCurrentWave = null;
                }
            }
            
        }
    }
}
